//
//  NSString+LCMD5.m
//  UI_24_加密
//
//  Created by li cheng on 16/8/16.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "NSString+LCMD5.h"
#import <CommonCrypto/CommonCrypto.h>
@implementation NSString (LCMD5)
// 字符串转换, 32位MD5字符串小写
- (NSString *)stringWith32BitMD5Lower{
    const char *cString = [self UTF8String];
    CC_LONG length = (CC_LONG)strlen(cString);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    // 2.将 C 的字符串转换 MD5
    CC_MD5(cString, length, bytes);
    // 3.将 C 的字符串转换层OC的字符串
    NSMutableString *finalMD5String = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++){
        [finalMD5String appendFormat:@"%02x", bytes[i]];
    }
    return finalMD5String;
}

- (NSString *)stringWith32BitMD5{
    const char *cString = [self UTF8String];
    CC_LONG length = (CC_LONG)strlen(cString);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    // 2.将 C 的字符串转换 MD5
    CC_MD5(cString, length, bytes);
    // 3.将 C 的字符串转换层OC的字符串
    NSMutableString *finalMD5String = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++){
        [finalMD5String appendFormat:@"%02x", bytes[i]];
    }
    return finalMD5String;
}

// 字符串转换, 32位MD5字符串大写
- (NSString *)stringWith32BitMD5Upper{
    return [[self stringWith16BitMD5] uppercaseString];
}

- (NSString *)stringWith16BitMD5 {
    NSString *md5String32Bit = [self stringWith32BitMD5];
    NSString *md5String16Bit = [md5String32Bit substringToIndex:md5String32Bit.length - 8];
    md5String16Bit = [md5String16Bit substringFromIndex:8];
    return md5String16Bit;
}
- (NSString *)stringWith16BitMD5Upper {
    return @"";
}
// 字符串转换, 16位MD5字符串小写
- (NSString *)stringWith16BitMD5Lower {
    return  @"";
}




@end

